package com.shopping.mall.core.module.mainPage.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.shopping.mall.auth.annotation.UnLogin;
import com.shopping.mall.base.http.Result;
import com.shopping.mall.core.module.mainPage.entity.MainPage;
import com.shopping.mall.core.module.mainPage.entity.MainPageVo;
import com.shopping.mall.core.module.mainPage.service.MainPageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@Api(tags = "MainPage")
@RestController
@RequestMapping("/mainPage")
public class MainPageController {
    @Autowired
    private MainPageService mainPageService;

    @ApiOperation("只添加,全部属性都不能为空")
    @PostMapping("/add")
    @PreAuthorize("hasAnyRole('SUPER_ADMIN','NORMAL_ADMIN')")
    public Result<Object> add(@Valid @RequestBody MainPageVo mainPageVo){
        return mainPageService.add(mainPageVo);
    }

    @ApiOperation("返回最新的数据")
    @GetMapping("/last")
    @UnLogin
    public Result<Object> select(){
        return mainPageService.select();
    }

    @ApiOperation("分页查询")
    @GetMapping("/page")
    public Result<IPage<MainPage>> getMainPagePage(
        @RequestParam(name = "current", defaultValue = "1") Integer current,
        @RequestParam(name = "size", defaultValue = "10") Integer size,
        MainPage mainPage) {
        IPage<MainPage> mainPagePage=mainPageService.getMainPagePage(current,size,mainPage);
        return Result.OK(mainPagePage);
    }

    @ApiOperation("修改")
    @PutMapping
    public Result<MainPage> update(@Validated(MainPage.update.class) @RequestBody MainPage mainPage) {
        mainPageService.updateById(mainPage);
        return Result.OK();
    }

    @ApiOperation("新增")
    @PostMapping
    public Result<Object> add(@Validated(MainPage.insert.class) @RequestBody MainPage mainPage) {
        mainPageService.save(mainPage);
        return Result.OK();
    }

    @ApiOperation("通过id查询")
    @GetMapping("/id")
    public Result<MainPage> getById(@RequestParam String id){
        MainPage mainPage=mainPageService.getById(id);
        return Result.OK(mainPage);
    }

    @ApiOperation("查询所有")
    @GetMapping
    public Result<List<MainPage>> getAll(MainPage mainPage){
        List<MainPage> mainPageList=mainPageService.getAll(mainPage);
        return Result.OK(mainPageList);
    }

    @ApiOperation("通过id删除")
    @DeleteMapping("/id")
    public Result<Object> deleteById(@RequestParam String id){
        return mainPageService.deleteById(id);
    }

    @ApiOperation("批量删除")
    @DeleteMapping("/batch")
    public Result<Object> batchDelete(@RequestParam List<String> ids){
        return mainPageService.batchDelete(ids);
    }
}
